package org.oschina.handler.validator.filter;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.oschina.api.upload.service.UploadConfigService;
import org.oschina.api.upload.vo.UploadConfigVo;
import org.oschina.handler.validator.IFilter;
import org.oschina.handler.validator.ValidateBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 上传验证
 */
@Component
public class UploadFilter implements IFilter {

	@Autowired
	private UploadConfigService uploadConfigService;
	
	@Override
	public void execute(ValidateBean validateBean)
	{
		if(StringUtils.isBlank(validateBean.getValue()))
			return;
		
		List<UploadConfigVo> uploadConfigList=uploadConfigService._findVo();
		
		String apart[]=StringUtils.split(validateBean.getValue(),"/");
		for(int i=0,len=uploadConfigList.size();i<len;i++)
		{
			UploadConfigVo uploadConfigVo=uploadConfigList.get(i);
			String configApart[]=StringUtils.split(uploadConfigVo.getPath(),"/");
			
			if(configApart.length==apart.length-1)
			{
				boolean pass=true;
				for(int j=0;j<configApart.length;j++)
				{
					if(!apart[j].equals(configApart[j]))
					{
						pass=false;
						break;
					}
				}
				if(pass)
					break;
			}
			
			if(i==len-1)
				validateBean.setValue(null);
		}
	}

}